package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

/**
 * @author mrchen
 * @date 2022/6/16 15:53
 */
@SpringBootTest
public class EsDocumentDemo {
    @Autowired
    IHotelService iHotelService;

    @Test
    public void addDocument() throws IOException {
        // 1. 从数据库中根据id查询一条文档数据 ==> Hotel
        Hotel hotel = iHotelService.getById(36934);
        // 2. 将Hotel对象  转为 ==> HotelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        // 3. 将hotelDoc对象保存到 ES索引库
        // 3.1  创建添加文档的请求对象
        IndexRequest request = new IndexRequest("hotel");
        // 设置文档id
        request.id(hotelDoc.getId() + "");
        // 设置文档内容
        String jsonStr = JSON.toJSONString(hotelDoc);
        request.source(jsonStr, XContentType.JSON);
        // 3.2  调用客户端执行请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        // 3.3  得到相应结果
        System.out.println(response.getResult());
    }

    @Test
    public void getDocumentById() throws IOException {
        // 36934
        // 1. 创建查询文档的请求对象
        GetRequest request = new GetRequest("hotel","36934");
        // 2. 执行查询文档请求
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3. 解析响应得到文档结果
        String hotelJsonStr = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(hotelJsonStr, HotelDoc.class);
        System.out.println(hotelDoc);
    }
    @Test
    public void updateById() throws IOException {
        // 36934
        // 1. 创建修改文档的请求对象
        UpdateRequest request = new UpdateRequest("hotel","36934");
        request.doc("price","398");
        // 2. 执行修改文档请求
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        // 3. 解析响应得到文档结果
        System.out.println(response.getResult());
    }
    @Test
    public void deleteById() throws IOException {
        // 36934
        // 1. 创建删除文档的请求对象
        DeleteRequest request = new DeleteRequest("hotel","36934");
        // 2. 执行删除文档请求
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        // 3. 解析响应得到删除文档结果
        System.out.println(response.getResult());
    }


    @Test
    public void bulkAddDocument() throws IOException {
        // 1. 查询所有数据库中的酒店数据   查询全部
        List<Hotel> list = iHotelService.list();
        // 2. 批量的添加到 es索引库中
        BulkRequest bulkRequest = new BulkRequest("hotel");
        for (Hotel hotel : list) {
            // 3. for循环遍历 要批量插入的酒店列表，每一个酒店数据 都封装成一个IndexRequest对象
            IndexRequest request = new IndexRequest();
            request.id(hotel.getId() + ""); // 设置文档id
            HotelDoc hotelDoc = new HotelDoc(hotel);
            request.source(JSON.toJSONString(hotelDoc),XContentType.JSON); // 设置文档内容
            // 4. 将 添加文档的请求  加入到 批处理请求中
            bulkRequest.add(request);
        }
        // 5. 执行批处理请求
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        // 打印结果  OK代表成功      GET 索引库/_search
        System.out.println(bulk.status());
    }



    RestHighLevelClient client;
    @BeforeEach
    public void init(){
        // 1. 准备好Es的客户端对象
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.200.130:9200")
        ));
    }
    @AfterEach
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
